package com.hunliji.monitor.web.controller.app;

import com.alibaba.fastjson.JSON;
import com.hunliji.common.response.ResponseData;
import com.hunliji.monitor.manager.api.FeiShuManager;
import com.hunliji.monitor.manager.api.model.param.FeiShuSendMsgManagerParam;
import com.hunliji.monitor.web.facade.FeiShuFacade;
import com.hunliji.monitor.web.model.param.feishu.InterceptPluginParam;
import com.hunliji.monitor.web.model.param.feishu.NotifyParam;
import com.hunliji.monitor.web.model.vo.NotifyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 飞书包装接口
 * @author wuchen
 */
@RestController
@RequestMapping(value = "/appApi/feishu")
@Slf4j
public class FeiShuAppController {
    @Resource
    private FeiShuFacade feiShuFacade;
    /**
     * app_ticket 事件 消息推送，获取到对应的值可以去获取token
     * @param notifyParam
     * @return
     */
    @PostMapping("/notify")
    public NotifyVO notify(@RequestBody NotifyParam notifyParam) throws Exception {
        log.info("notify-接收到notify 事件 消息推送，appTicketNotifyParam:{}", JSON.toJSONString(notifyParam));
        NotifyVO notifyVO = feiShuFacade.handleNotify(notifyParam);
        log.info("notify-处理notify事件，响应结果notifyVO：{}",JSON.toJSONString(notifyVO));
        return notifyVO;
    }


    /**
     * 飞书拦截事件通知
     */
    @PostMapping("/interception/notify")
    public Void interceptionNotify(@RequestBody InterceptPluginParam param) {
        log.info("飞书拦截事件通知事件 消息推送，appTicketNotifyParam:{}", JSON.toJSONString(param));
        feiShuFacade.handleInterceptionNotify(param.getData());
        return null;
    }

    /**
     * 飞书延迟事件取消
     */
    @GetMapping("/interception/delayCancel")
    public ResponseData<Boolean> delayCancel(@RequestParam("workItemId") Long workItemId,
                                             @RequestParam("type") Integer type) {
        feiShuFacade.delayCancel(workItemId, type);
        return ResponseData.success(true);
    }

    /**
     * 获取飞书token
     * @return
     */
    @GetMapping("/obtainToken")
    public ResponseData obtainToken(){
        feiShuFacade.handleToken();
        return ResponseData.success(null);
    }


    /**
     * 处理群组
     * @return
     */
    @PostMapping("/handleChatGroup")
    public ResponseData handleChatGroup(){
        feiShuFacade.handleChatGroup();
        return ResponseData.success(null);
    }
    @Resource
    private FeiShuManager feiShuManager;
    @PostMapping("/handleToOne")
    public ResponseData handleToOne(@RequestBody FeiShuSendMsgManagerParam feiShuSendMsgManagerParam,@RequestParam("token") String token){
        feiShuManager.sendMsg(feiShuSendMsgManagerParam,token);
        return ResponseData.success(null);
    }


    @GetMapping("/createDefect")
    public ResponseData createDefect(@RequestParam("title") String title,@RequestParam("content") String content){
        feiShuFacade.createDefect(title,content);
        return ResponseData.success(true);
    }
}
